{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<center>\n",
    "    <p style=\"text-align:center\">\n",
    "        <img alt=\"phoenix logo\" src=\"https://raw.githubusercontent.com/Arize-ai/phoenix-assets/9e6101d95936f4bd4d390efc9ce646dc6937fb2d/images/socal/github-large-banner-phoenix.jpg\" width=\"1000\"/>\n",
    "        <br>\n",
    "        <br>\n",
    "        <a href=\"https://arize.com/docs/phoenix/\">Docs</a>\n",
    "        |\n",
    "        <a href=\"https://github.com/Arize-ai/phoenix\">GitHub</a>\n",
    "        |\n",
    "        <a href=\"https://arize-ai.slack.com/join/shared_invite/zt-2w57bhem8-hq24MB6u7yE_ZF_ilOYSBw#/shared-invite/email\">Community</a>\n",
    "    </p>\n",
    "</center>\n",
    "<h1 align=\"center\">LangChain Tracing</h1>\n",
    "\n",
    "Let's see how to get started with [LangChain.js](https://js.langchain.com/docs/introduction/) and [OpenInference](https://github.com/Arize-ai/openinference/tree/main/js) to trace your LangChain application using Deno.\n",
    "\n",
    "> Note: that this example requires the OPENAI_API_KEY environment variable to be set and assumes you are running the Phoenix server on localhost:6006.\n",
    "\n",
    "In order to run this notebook please run the following command from this directory:\n",
    "```shell\n",
    "npm install\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import {\n",
    "  NodeTracerProvider,\n",
    "  SimpleSpanProcessor,\n",
    "} from \"@opentelemetry/sdk-trace-node\";\n",
    "import { Resource } from \"@opentelemetry/resources\";\n",
    "import { OTLPTraceExporter } from \"@opentelemetry/exporter-trace-otlp-proto\";\n",
    "import { SEMRESATTRS_PROJECT_NAME } from \"@arizeai/openinference-semantic-conventions\";\n",
    "\n",
    "const provider = new NodeTracerProvider({\n",
    "  resource: new Resource({\n",
    "    [SEMRESATTRS_PROJECT_NAME]: \"deno-langchain\",\n",
    "  }),\n",
    "});\n",
    "\n",
    "provider.addSpanProcessor(\n",
    "  new SimpleSpanProcessor(\n",
    "    new OTLPTraceExporter({\n",
    "      url: \"http://localhost:6006/v1/traces\",\n",
    "    })\n",
    "  )\n",
    ");\n",
    "\n",
    "provider.register();\n",
    "\n",
    "console.log(\"👀 OpenInference initialized\");\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import * as lcCallbackManager from \"@langchain/core/callbacks/manager\";\n",
    "import { LangChainInstrumentation } from \"@arizeai/openinference-instrumentation-langchain\";\n",
    "\n",
    "const lcInstrumentation = new LangChainInstrumentation();\n",
    "lcInstrumentation.manuallyInstrument(lcCallbackManager);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import { ChatOpenAI } from \"@langchain/openai\";\n",
    "import { ChatPromptTemplate } from \"@langchain/core/prompts\";\n",
    "\n",
    "const PROMPT_TEMPLATE = `\n",
    "    You are a highly trained AI model that can answer any question. Please answer questions concisely. \n",
    "    Answer the following question: \n",
    "    \"{question}\"\n",
    "`;\n",
    "\n",
    "const question = \"What is javascript used for?\";\n",
    "\n",
    "const chatPrompt = ChatPromptTemplate.fromTemplate(PROMPT_TEMPLATE)\n",
    "\n",
    "const model = new ChatOpenAI({ model: \"gpt-4\" });\n",
    "\n",
    "const chain = chatPrompt.pipe(model);\n",
    "\n",
    "const response = await chain.invoke({ question });\n",
    "\n",
    "console.log(response.content);\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "typescript"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
